class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == nullptr) {
            return nullptr; // If the root is null, return null.
        }
        
        queue<TreeNode*> que;
        que.push(root); // Start with the root node.
        
        while (!que.empty()) {
            TreeNode* cur = que.front();
            que.pop();
            
            // Swap the left and right children.
            TreeNode* temp = cur->left;
            cur->left = cur->right;
            cur->right = temp;
            
            // Add the children to the queue if they are not null.
            if (cur->left) {
                que.push(cur->left);
            }
            if (cur->right) {
                que.push(cur->right);
            }
        }
        
        return root; // Return the root of the inverted tree.
    }
};